package cn.regexp.business.web.controller;


import cn.hutool.core.util.ObjectUtil;
import cn.regexp.common.base.page.TablePageInfo;
import cn.regexp.common.web.AjaxResult;
import cn.regexp.domain.ShopGoods;
import cn.regexp.domain.ShopWindow;
import cn.regexp.query.GoodsQuery;
import cn.regexp.service.IGoodsService;
import cn.regexp.service.IWindowService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Collections;

/**
 * 商品控制器
 */
@Controller
@RequestMapping("business/goods")
public class GoodsController {
    //模板前缀
    private static final String prefix = "business/goods/";

    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private IWindowService windowService;

    //页面------------------------------------------------------------
    //列表
    @RequiresPermissions("business:goods:view")
    @RequestMapping("/listPage")
    public String listPage(Long windowId, Model model){
        model.addAttribute("windows", windowService.list());
        model.addAttribute("window", windowService.getById(windowId));
        return prefix + "list";
    }

    @RequiresPermissions("business:goods:add")
    @RequestMapping("/addPage")
    public String addPage(Model model){
        model.addAttribute("windows", windowService.list());
        return prefix + "add";
    }

    @RequiresPermissions("business:goods:edit")
    @RequestMapping("/editPage")
    public String editPage(Long id, Model model){
        model.addAttribute("windows", windowService.list());
        model.addAttribute("good", goodsService.getById(id));
        return prefix + "edit";
    }

    //数据-----------------------------------------------------------
    //列表
    @RequiresPermissions("business:goods:list")
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<ShopGoods> query(GoodsQuery qo){
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        QueryWrapper<ShopGoods> wrapper = new QueryWrapper<>();
        wrapper.eq(ObjectUtil.isNotEmpty(qo.getWid()), "window_id", qo.getWid())
                .eq(ObjectUtil.isNotEmpty(qo.getState()), "state", qo.getState())
                .eq(ObjectUtil.isNotEmpty(qo.getIsHot()), "is_hot", qo.getIsHot())
                .eq(ObjectUtil.isNotEmpty(qo.getSaleState()), "sale_state", qo.getSaleState())
                .like(ObjectUtil.isNotEmpty(qo.getKeyword()), "goods_name", qo.getKeyword());
        return new TablePageInfo<>(goodsService.list(wrapper), goodsService.count(wrapper));
    }
    //新增
    @RequiresPermissions("business:goods:add")
    @RequestMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ShopGoods goods){
        ShopWindow shopWindow = windowService.getById(goods.getWindowId());
        goods.setWindowName(shopWindow.getWindowName());
        goodsService.save(goods);
        return AjaxResult.success();
    }
    //编辑
    @RequiresPermissions("business:goods:edit")
    @RequestMapping("/edit")
    @ResponseBody
    public AjaxResult edit(ShopGoods goods){
        goodsService.updateById(goods);
        return AjaxResult.success();
    }
    //删除
    @RequiresPermissions("business:goods:remove")
    @RequestMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids){
        goodsService.removeByIds(Collections.singletonList(ids));
        return AjaxResult.success();
    }
}
